﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using RickDnn.Modules.DNNTaskManager.Data;

namespace RickDnn.Modules.DNNTaskManager.Components
{
    public class TaskController
    {
        public static Task GetTask(int taskId)
        {
            //FillOject
            return CBO.FillObject<Task>(DataProvider.Instance().GetTask(taskId));
        }
        public static List<Task> GetTasks(int moduleId)
        {
            //FillOject
            return CBO.FillCollection<Task>(DataProvider.Instance().GetTasks(moduleId));
        }

        public static void DeleteTask(int taskId)
        {            
             DataProvider.Instance().DeleteTask(taskId);
        }
        public static void DeleteTasks(int moduleId)
        {
            DataProvider.Instance().DeleteTasks(moduleId);
        }

        public static int SaveTask(Task t,int tabId)
        {
            //t.TaskId<1 means is new
            if (t.TaskId<1)
            {
                t.TaskId = DataProvider.Instance().AddTask(t);// taskId is from sql
                //add content Item integration
                var cntTaxonomy = new Taxonomy.Content();
                var objContentItem = cntTaxonomy.CreateContentItem(t, tabId);
                t.ContentItemId = objContentItem.ContentItemId;
                //Check where is the Error 
                //t.ContentItemId = 1;

                //reccursive
                SaveTask(t,tabId);
            }
            else
            {
                DataProvider.Instance().UpdateTask(t);
                var cntTaxonomy = new Taxonomy.Content();
                cntTaxonomy.UpdateContentItem(t, tabId);
            }
            return t.TaskId;
        }      
    }
}